﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class EquipmentBorrowService : BaseService, IEquipmentBorrowService
    {
        private readonly IEquipmentBorrowRepository _equipmentBorrowRepository;
        private readonly IUnitOfWork _unitOfWork;
        public EquipmentBorrowService(IMapper mapper, IEquipmentBorrowRepository _equipmentBorrowRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._equipmentBorrowRepository = _equipmentBorrowRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddEquipmentBorrow(EquipmentBorrow equipmentBorrow)
        {
            _equipmentBorrowRepository.AddEntity(equipmentBorrow);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelEquipmentBorrow(long id)
        {
            var cust = _equipmentBorrowRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _equipmentBorrowRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<EquipmentBorrow> GetEquipmentBorrow(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _equipmentBorrowRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                custList = custList.Where(c => c.Name.Contains(name));
            }
            var list = _equipmentBorrowRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public EquipmentBorrow? GetEquipmentBorrowById(long id)
        {
            return _equipmentBorrowRepository.QueryEntityById(id);
        }

        public bool UpdEquipmentBorrow(EquipmentBorrow equipmentBorrow)
        {
            var aobj = _equipmentBorrowRepository.QueryIsEntityWhere(x => x.Id == equipmentBorrow.Id);
            if (aobj == null)
            {
                return false;
            }
            _equipmentBorrowRepository.UpdEntity(equipmentBorrow);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
